package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.UserPointBean;
import com.cljz.makepassenger.ui.activity.PointActivity;
import com.cljz.makepassenger.utils.L;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

public class PointP extends BaseP<PointActivity> {

    public void getPointList(int page) {
        HashMap<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("token", getToken());
        String body = Base64Utils.enCode(gson.toJson(map));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().userPointList(requestBody)
                .compose(XApi.<UserPointBean>getApiTransformer())
                .compose(XApi.<UserPointBean>getScheduler())
                .compose(getV().<UserPointBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<UserPointBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().LoadDataError(error.toString());
                        L.e(error.toString());
                    }

                    @Override
                    public void onNext(UserPointBean userPointBean) {
                        L.e(userPointBean.getData().toString());
                        if ("0".equals(userPointBean.getError_code())) {
                            getV().LoadDataSuccess(userPointBean.getData());
                        } else if ("1000".equals(userPointBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().LoadDataError(userPointBean.getMsg());
                        }
                    }
                });
    }
}
